package com.redhipps.hips.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.Response;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.redhipps.hips.client.command.ListInstitutionsCommand;
import com.redhipps.hips.client.io.IOServiceFactory;
import com.redhipps.hips.client.io.RequestBuilderFactory;
import com.redhipps.hips.client.model.Context;
import com.redhipps.hips.client.model.Doctor;
import com.redhipps.hips.client.model.ModelRepository;
import com.redhipps.hips.client.widget.Dialogs;
import com.redhipps.hips.client.widget.MenuBarBuilder;

/**
 * Entry point.
 */
public class HipsApp implements EntryPoint {

  // TODO Find a better place for this.
  private static ModelRepository<Doctor> doctorRepository;
  private static RequestBuilderFactory requestBuilderFactory;
  private static IOServiceFactory ioServiceFactory;

  /**
   * This is the entry point method.
   */
  public void onModuleLoad() {
    requestBuilderFactory = new RequestBuilderFactory();
    ioServiceFactory = new IOServiceFactory(requestBuilderFactory);

    MenuBarBuilder menuBarBuilder = new MenuBarBuilder();
    setHeader(menuBarBuilder.create());

    VerticalPanel initialPanel = new VerticalPanel();
    initialPanel.setHorizontalAlignment(VerticalPanel.ALIGN_CENTER);
    initialPanel.setSpacing(8);
    Button aboutButton = new Button("About");
    initialPanel.add(aboutButton);
    aboutButton.addClickListener(new ClickListener() {
      public void onClick(Widget sender) {
        Dialogs.info("About HIPS",
            "HIPS version 0.1a\n" +
            "PRIVATE BETA\n" +
            "DO NOT DISTRIBUTE", null);
      }
    });
    setContents(initialPanel);

    Context.initialize(requestBuilderFactory,
        new RequestCallback() {
          public void onError(Request request, Throwable exception) {
            GWT.log("Error in context load.", exception);
          }
          public void onResponseReceived(Request request, Response response) {
            contextLoadComplete();
          }
        });
  }

  private void contextLoadComplete() {
    new ListInstitutionsCommand().execute();
  }
  
  public static void setHeader(Widget header) {
    RootPanel cp = RootPanel.get("header");
    cp.clear();
    cp.add(header);
  }

  public static void setContents(Widget contents) {
    RootPanel cp = RootPanel.get("content");
    cp.clear();
    cp.add(contents);
  }

  // TODO Global stuff. Pass this around better.

  public static ModelRepository<Doctor> doctorRepository() {
    return doctorRepository;
  }

  public static RequestBuilderFactory requestBuilderFactory() {
    return requestBuilderFactory;
  }

  public static IOServiceFactory ioServiceFactory() {
    return ioServiceFactory;
  }

  // For testing

  public static void setIoServiceFactory(IOServiceFactory ioServiceFactory) {
    HipsApp.ioServiceFactory = ioServiceFactory;
  }

  public static void setDoctorRepository(ModelRepository<Doctor> doctorRepository) {
    HipsApp.doctorRepository = doctorRepository;
  }

  public static void setRequestBuilderFactory(
      RequestBuilderFactory requestBuilderFactory) {
    HipsApp.requestBuilderFactory = requestBuilderFactory;
  }

}
